let logger = require("pomelo-logger").getLogger(__filename);
let DateUtil = require("../../../../utils/dateUtil");
let COMPONENT_NAME = "statComponent";

StatComponent = function (owner) {
    this.owner = owner;
    this.playTimes = 0;
    this.sharedTime = [];
};

StatComponent.prototype.getPlayTimes = function () {
    return this.playTimes;
};

StatComponent.prototype.addPlayTimes = function () {
    return this.playTimes ++;
};

StatComponent.prototype.getSharedTime = function () {
    return this.sharedTime;
};

StatComponent.prototype.addShareTime = function (ts) {
    this.sharedTime.push(ts);
};

StatComponent.prototype.fromBSON = function (document) {
    if (!document.hasOwnProperty(COMPONENT_NAME)) {
        logger.error("load statComponent failed. document=" + JSON.stringify(document));
        return;
    }
    let root = document[COMPONENT_NAME];
    this.playTimes = root["playTimes"] || 0;
    this.sharedTime = root["sharedTime"] || [];
};

StatComponent.prototype.buildBSON = function (document) {
   document[COMPONENT_NAME] = {
       playTimes: this.playTimes,
       sharedTime: this.sharedTime,
   };
};

StatComponent.prototype.toJSON = function () {
    return {
        playTimes : this.playTimes,
        sharedTime : this.sharedTime,
    };
};

module.exports.create = function (owner) {
    return new StatComponent(owner);
};
